package com.t4.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.t4.auth.service.SysUserService;
import com.t4.common.exception.T4Exception;
import com.t4.model.process.Process;
import com.t4.model.process.ProcessTemplate;
import com.t4.model.system.SysUser;
import com.t4.process.service.ProcessService;
import com.t4.process.service.ProcessTemplateService;
import com.t4.security.custom.LoginUserInfoHelper;
import com.t4.wechat.service.WechatMessageService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class WechatMessageServiceImpl implements WechatMessageService {

    @Resource
    private ProcessService processService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private ProcessTemplateService processTemplateService;

    @Override
    public void pushPendingMessage(Long processId, Long userId, String taskId) {
        // 查询流程信息
        Process process = processService.getById(processId);
        // 查询要推送人信息
        SysUser sysUser = sysUserService.getById(userId);
        // 查询审批模板信息
        ProcessTemplate processTemplate = processTemplateService.getById(process.getProcessTemplateID());
        // 查询审批发起人信息
        SysUser submitUser = sysUserService.getById(process.getUserId());
        // 获取要推送人的openId
        String openId = sysUser.getOpenId();
        if(StringUtils.isEmpty(openId)){
            throw new T4Exception(2017, "推送人未绑定微信号");
        }
        // 设置消息发送信息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId) // 要推送人的用户openId
                .templateId("wKBiWXPYRxCK9jE-4u5HdDj9TaIlatNhtaK7kSZ8eas") // 推送模板id
                .url("http://127.0.0.1:8800/#/show/" + processId + "/" + taskId) // 点击推送消息后跳转的地址
                .build();
        JSONObject jsonObject = JSON.parseObject(process.getFormValues());
        JSONObject formShowData = jsonObject.getJSONObject("formShowData");
        StringBuffer content = new StringBuffer();
        for (Map.Entry entry : formShowData.entrySet()) {
            content.append(entry.getKey()).append("：").append(entry.getValue()).append("\n ");
        }
        templateMessage.addData(new WxMpTemplateData("first", submitUser.getName()+"提交了"+processTemplate.getName()+"审批申请，请注意查看。", "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword1", process.getProcessCode(), "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword2", new DateTime(process.getCreateTime()).toString("yyyy-MM-dd HH:mm:ss"), "#272727"));
        templateMessage.addData(new WxMpTemplateData("content", content.toString(), "#272727"));
    }

    @Override
    public void pushProcessedMessage(Long processId, Long userId, Integer status) {
        Process process = processService.getById(processId);
        ProcessTemplate processTemplate = processTemplateService.getById(process.getProcessTemplateID());
        SysUser sysUser = sysUserService.getById(userId);
        SysUser currentSysUser = sysUserService.getById(LoginUserInfoHelper.getUserId());
        String openid = sysUser.getOpenId();
        if(StringUtils.isEmpty(openid)) {
            throw new T4Exception(2017, "推送人未绑定微信号");
        }
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)//要推送的用户openid
                .templateId("CvOVvAZ6ZGXbTBi4UL0deAVwXJYAWER_uTs8oL8i7PA")//模板id
                .url("http://127.0.0.1:8800/#/show/"+processId+"/0")//点击模板消息要访问的网址
                .build();
        JSONObject jsonObject = JSON.parseObject(process.getFormValues());
        JSONObject formShowData = jsonObject.getJSONObject("formShowData");
        StringBuffer content = new StringBuffer();
        for (Map.Entry entry : formShowData.entrySet()) {
            content.append(entry.getKey()).append("：").append(entry.getValue()).append("\n ");
        }
        templateMessage.addData(new WxMpTemplateData("first", "你发起的"+processTemplate.getName()+"审批申请已经被处理了，请注意查看。", "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword1", process.getProcessCode(), "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword2", new DateTime(process.getCreateTime()).toString("yyyy-MM-dd HH:mm:ss"), "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword3", currentSysUser.getName(), "#272727"));
        templateMessage.addData(new WxMpTemplateData("keyword4", status == 1 ? "审批通过" : "审批拒绝", status == 1 ? "#009966" : "#FF0033"));
        templateMessage.addData(new WxMpTemplateData("content", content.toString(), "#272727"));
    }
}
